﻿<?php
require_once("header.php");
require_once("controls/registernewsletter.class.php");
require_once("controls/ajax.php");
$newsletter = new RegisterNewsletterClass();
$ajax = new Ajax();
?>
<script type="text/javascript">
$(document).ready(function(e) {
	$("#submitNewsletter").live("click", function() {
		var error = $("#error").html();
		var email = $("#email").val();
		if(email.length > 0 && isValidEmailAddress(email)) {
			$.post('controls/ajax.php',{mode: "newsletter", email: email}, function(data){
				if(data > 1){
					$("#error").html(email + " finns redan i databasen");
				}else{
					$("#error").html(email + " är registrerad i databasen");
					//här stänge vi popup sen
				}
			});
		}
		else {
			if(email.length == 0){
				$("#error").html("Får inte vara tom");
			}
			else if(!isValidEmailAddress(email)){
				$("#error").html("Måste vara en giltlig e-postadress");
			}
		}
	}, 'JSON');
	/*
	check if email address is valid
	 */
	function isValidEmailAddress(emailAddress) {
    	var pattern = RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
	/* end check if email address is valid */
};
});
</script>
<div class="container_12">
    <div class="grid_12">
        <div class="newsletter">
        <h3>Nyhetsbrev</h3>
            <input type="text" name="email" id="email" />
            <input type="submit" name="submit" id="submitNewsletter" /><span id="error"></span>
        <?php
			
		?>
        </div>
    </div>
</div>
<?php
require_once("footer.php");
?>